package top.likeqc.leetcode.editor.cn;

/**
 * @see <a
 *     href="https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate">leetcode题库</a>
 */
public class FindNearestPointThatHasTheSameXOrYCoordinate {
    public static void main(String[] args) {
        Solution solution = new FindNearestPointThatHasTheSameXOrYCoordinate().new Solution();
    }
    // leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int nearestValidPoint(int x, int y, int[][] points) {
            int minDiff = Integer.MAX_VALUE;
            int minIndex = -1;
            for (int i = 0; i < points.length; i++) {
                int m = points[i][0];
                int n = points[i][1];
                if (x == m || y == n) {
                    int dist = Math.abs(x - m) + Math.abs(y - n);
                    if (dist < minDiff) {
                        minDiff = dist;
                        minIndex = i;
                    }
                }
            }
            return minIndex;
        }
    }
    // leetcode submit region end(Prohibit modification and deletion)

}
